package cn.com.idmy.cloud.config;

import cn.com.idmy.cloud.util.CloudUtil;
import cn.com.idmy.cloud.util.SpringUtil;
import com.alibaba.fastjson2.JSON;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.thread.ThreadUtil;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.Async;

@AutoConfiguration
@Import(SameHostLoadBalancerConfiguration.class)
public class DmyWebCloudConfig implements SmartInitializingSingleton {
    @Async
    @Override
    public void afterSingletonsInstantiated() {
        CloudUtil.setLoadBalancerClient(SpringUtil.getBean(LoadBalancerClient.class));
        ThreadUtil.sleep(3000);
        var discoveryClient = SpringUtil.getBean(DiscoveryClient.class);
        var services = discoveryClient.getServices();
        for (var service : services) {
            Console.error("nacos【" + service + "】", JSON.toJSONString(discoveryClient.getInstances(service)));
        }
    }
}
